package m202405.d11;

import java.util.HashSet;
import java.util.Set;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/11 14:03
 */
public class Q3 {

    public static void main(String[] args) {
        Q3 q3 = new Q3();
        q3.lengthOfLongestSubstring("ab");
    }

    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        int max = 1;
        int left = 0;
        Set<Character> set = new HashSet<>();
        set.add(s.charAt(0));
        for (int i = 1; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!set.contains(c)) {
                set.add(c);
                continue;
            }
            max = Math.max(max, set.size());
            while (set.contains(c)) {
                set.remove(s.charAt(left));
                left++;
            }
            set.add(c);
        }
        return Math.max(max, set.size());
    }
}
